package com.hsq.ndmall.wx.service;

import cn.hutool.core.collection.ListUtil;
import com.hsq.ndmall.common.utils.ListFromEntity2VoUtils;
import com.hsq.ndmall.common.utils.R;
import com.hsq.ndmall.wx.feign.catalog.ProductFeign;
import com.hsq.ndmall.wx.feign.goods.GoodsDetailFeign;
import com.hsq.ndmall.wx.feign.goods.MallIssueFeign;
import com.hsq.ndmall.wx.feign.goods.PromotionFeign;
import com.hsq.ndmall.wx.vo.goods.*;
import com.hsq.ndmall.wx.vo.groupon.GrouponRulesVo;
import com.hsq.ndmall.wx.vo.mall.IssueVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 黄四清
 * @version 1.0.0
 * @ClassName GoodsService.java
 * @Description TODO 商品详情
 * @createTime 2021年08月16日 09:21:00
 */
@Service
public class GoodsService {

    @Autowired
    GoodsDetailFeign goodsDetailFeign;

    @Autowired
    MallIssueFeign mallIssueFeign;

    @Autowired
    PromotionFeign promotionFeign;

    @Autowired
    ProductFeign productFeign;

    public Map detail(Integer id) {
        Map resultMap = new HashMap();
        funAll(resultMap, id);
        return resultMap;
    }

    // 商品详情
    private void funAll(Map resultMap, Integer id) {
        Map map = new HashMap<String, Object>();
        map.put("goods_id", id);
        // TODO 1.规格  specificationList
        R specificationListR = goodsDetailFeign.specificationList(map);
        List<GoodsSpecificationVo> goodsSpecificationVoList = new ArrayList<>();
        List<Map> specificationList = (List<Map>) ((Map) specificationListR.get("page")).get("list");
        // 拿到所有规格
        List<Specification> specificationsAll = ListFromEntity2VoUtils.entity2Vo(specificationList, Specification.class);
        // 用来存放规格名字名字，（利用set的去重）
        Set<String> spNameSet = new HashSet<>();
        for (Specification el : specificationsAll) {
            spNameSet.add(el.getSpecification());
        }
        // 遍历规格名字
        for (String s : spNameSet) {
            // 创建GoodsSpecificationVo的实体类
            GoodsSpecificationVo goodsSpecificationVo = new GoodsSpecificationVo();
            // 创建规格名字相同的多个Specification的集合
            List<Specification> list = new ArrayList<>();
            // 将规格名字设置到 goodsSpecificationVo的name属性中
            goodsSpecificationVo.setName(s);
            // 遍历所有规格specifications
            for (Specification el : specificationsAll) {
                // 判断set集合的名字与所有规格名字相同
                if (el.getSpecification().equals(s)) {
                    // 添加到集合
                    list.add(el);
                }
                goodsSpecificationVo.setValueList(list);
            }
            goodsSpecificationVoList.add(goodsSpecificationVo);
        }
        resultMap.put("specificationList", goodsSpecificationVoList);


        // TODO 2.团购   groupon
        R grouponRulesRelateGoods = promotionFeign.groupRulesRelateGoods(id);
        List<Map> grouponList = (List<Map>) ((Map) grouponRulesRelateGoods.get("page")).get("list");
        List<GrouponRulesVo> grouponRulesVos = ListFromEntity2VoUtils.entity2Vo(grouponList, GrouponRulesVo.class);
        resultMap.put("groupon", grouponRulesVos);

        // TODO 3.常见问题  issue
        R issueListR = mallIssueFeign.list(map);
        List<Map> issueList = (List<Map>) ((Map) issueListR.get("page")).get("list");
        List<IssueVo> issueVos = ListFromEntity2VoUtils.entity2Vo(issueList, IssueVo.class);
        resultMap.put("issue", issueVos);

        // TODO 4. 商品评论列表 comment
        R commentListR = goodsDetailFeign.commentList(map);
        //Integer count = (Integer) ((Map)commentListR.get("page")).get("totalCount");
        List<Map> goodsCommentList = (List<Map>) ((Map) commentListR.get("page")).get("list");
        List<GoodsCommonVo> goodsCommonVos = ListFromEntity2VoUtils.entity2Vo(goodsCommentList, GoodsCommonVo.class);
        List<CommonVo> commonVosList = new ArrayList<>();
        CommonVo commonVo = new CommonVo();
        // 数量
        commonVo.setCount((Integer) ((Map) commentListR.get("page")).get("totalCount"));
        commonVo.setData(goodsCommonVos);
        commonVosList.add(commonVo);
        resultMap.put("comment", commonVosList);

        // TODO 5.商品属性 attribute
        R goodsAttributeR = goodsDetailFeign.goodsattributeList(map);
        List<Map> goodsAttributeList = (List<Map>) ((Map) goodsAttributeR.get("page")).get("list");
        List<GoodsAttributeVo> goodsAttributeVo = ListFromEntity2VoUtils.entity2Vo(goodsAttributeList, GoodsAttributeVo.class);
        resultMap.put("attribute", goodsAttributeVo);

        // TODO 6. 品牌详情 brand
        R brandListR = goodsDetailFeign.brandList(map);
        List<Map> brandList = (List<Map>) ((Map) brandListR.get("page")).get("list");
        List<brandVo> brandVos = ListFromEntity2VoUtils.entity2Vo(brandList, brandVo.class);
        if (brandVos == null || brandVos.size() < 1) {
            resultMap.put("brand", null);
        } else {
            resultMap.put("brand", brandVos.get(0));
        }


        // TODO 7. 货品(sku)列表信息 productList
        R goodsProductListR = goodsDetailFeign.goodsProductList(map);
        List<Map> goodsProductList = (List<Map>) ((Map) goodsProductListR.get("page")).get("list");
        List<GoodsProductVo> goodsProductVos = ListFromEntity2VoUtils.entity2Vo(goodsProductList, GoodsProductVo.class);
        resultMap.put("productList", goodsProductVos);


        // TODO 8.商品详情 info
        R infoR = goodsDetailFeign.info(id);
        List<Map> listMap = new ArrayList<>();
        listMap.add((Map) infoR.get("goods"));
        List<GoodsDetailVo> goodsDetailVos = ListFromEntity2VoUtils.entity2Vo(listMap, GoodsDetailVo.class);
        resultMap.put("info", goodsDetailVos.get(0));

        // TODO 9.是否分享 share
        resultMap.put("share", false);

        // TODO 10.分享的图片 shareImage
        resultMap.put("shareImage", "");

        // TODO 11. 是否为收藏 userHasCollect
        resultMap.put("userHasCollect", 0);
    }

    // 根据goodsId推荐同类商品（对应大家都在看）
    public Map related(Integer id) {
        Map resultMap = new HashMap();
        Map paramMap = new HashMap<String, String>();
        paramMap.put("goodsId", id);
        paramMap.put("page", 1);
        paramMap.put("limit", 6);
        R goodsListR = goodsDetailFeign.goodsList(paramMap);
        List<Map> goodList = (List<Map>) (((Map) goodsListR.get("page")).get("list"));
        List<NewGoodsVo> newGoodsVos = ListFromEntity2VoUtils.entity2Vo(goodList, NewGoodsVo.class);
        resultMap.put("total", ((Map) (goodsListR.get("page"))).get("totalCount"));
        resultMap.put("pages", ((Map) (goodsListR.get("page"))).get("totalPage"));
        resultMap.put("limit", ((Map) (goodsListR.get("page"))).get("pageSize"));
        resultMap.put("page", ((Map) (goodsListR.get("page"))).get("currPage"));
        resultMap.put("list", newGoodsVos);
        return resultMap;
    }

    // 根据二级分类查询对应商品
    public Map category(Integer id) {
        Map resultMap = new HashMap<String, List>();
        // 参数
        Map paramMap = new HashMap<String, String>();
        // TODO 1、currentCategory
        R currentInfoR = productFeign.currentInfo(id);
        resultMap.put("currentCategory", currentInfoR.get("category"));

        // TODO 2、brotherCategory
        paramMap.put("brother_id", id);
        R brotherCategoryR = productFeign.currentCategoryList(paramMap);
        resultMap.put("brotherCategory", ((Map) brotherCategoryR.get("page")).get("list"));

        // TODO 3、parentCategory
        paramMap.clear();
        paramMap.put("parent_id", id);
        R parentCategoryR = productFeign.currentCategoryList(paramMap);
        resultMap.put("parentCategory", ((Map) parentCategoryR.get("page")).get("list"));
        return resultMap;
    }

    public Map findList(Integer categoryId, Integer page, Integer limit) {

        // TODO 1、list   L2 下面的的商品
        Map result = new HashMap();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", page);
        paramMap.put("limit", limit);
        paramMap.put("category_id",categoryId);
        R goodsList = goodsDetailFeign.goodsList(paramMap);
        result.put("data", goodsList.get("page"));

        // TODO 2、filterCategoryList   查询每个二级分类的详情（有数据）
        // 查询出二级分类下面有商品的二级分类
        return result;
    }
}
